 package absolute_zero.com.urbox;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.telephony.gsm.SmsManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class File_transmission extends Activity {
	private String file_path;
	private String destination;
	private TextView header, status;
	private ProgressBar progress;
	private Button ok;
	
	/** Called when the activity is first created. */
	  @Override
	  public void onCreate(Bundle savedInstanceState) 
	  {
	      super.onCreate(savedInstanceState);
	      //Set the screen up
	      setContentView(R.layout.transfer);
	      //get date from bundle
	      Bundle UserInfo = getIntent().getExtras();
	      
	      //file path as a String
		  file_path = UserInfo.getString("filepath");
		  //Destination number
	      destination = UserInfo.getString("destination");
	      //Destination name
	      String name = (String) UserInfo.get("name");
	      
	      //screen elements
	      header = (TextView)this.findViewById(R.id.transferring_title);
	      status = (TextView)this.findViewById(R.id.transferring_details);
	      progress = (ProgressBar)this.findViewById(R.id.receiveBar);
	      ok = (Button)this.findViewById(R.id.ok_button);
	      
	      //Extract filename from path
	      File file = new File(file_path);
	      String file_name = file.getName();
	      //Set up screen
	      progress.setVisibility(4);
	      header.setText("Transferring \n" + file_name);
	      status.setText("Finding " + name);
	      
		  //Send SMS message
	      	status.setText("Pinging " + name);
		    SmsManager smsMgr = SmsManager.getDefault();
		    String sendTo = destination;
		    String myMessage = "URBox_IP_request\n" + file_name;
		    //Write log to file
		    try{ 
	            File newfile = new File(Environment.getExternalStorageDirectory() + "/urbox/log.txt");
	            //if file doesn't exist make the file
	            
	            	//append entry to end of file
	            	FileWriter datawriter = new FileWriter(newfile, newfile.exists()); 
	                Log.d("Appending to file", "true");
	            	BufferedWriter out = new BufferedWriter(datawriter); 
	                // More likely is going to be a loop that writes consecutive entries of data 
	                out.write(file_name.trim() + "\t" +  ContactLookup.numberToName(destination, File_transmission.this).trim() +  "\t" + "Waiting\n"); 
	                //remember to close what you open
	                out.close(); 
	            
	        }catch(IOException e){ 
	            Log.e("Whoops", "Can't write" + e.getMessage()); 
	        } 
		    //the sms has been sent
		    smsMgr.sendTextMessage(destination, null, myMessage, null, null);
		    
		    //Let the user know that the page can be closed now
		    status.setText("Waiting for reply.  You may now close this window");
		    
		    ok.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					finish();
				}
			});
	  }
	  
	  public String getFilePath(){
		  return file_path;
	  }
}
